package com.activiti.test;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class MyTestFork extends SpringAutoRollbackTransactionTester {
	@Autowired
	private RepositoryService repositoryService;
	
	@Autowired
	private RuntimeService runtimeService;
	
	@Autowired
	private TaskService taskService;
	
	@Autowired
	private IdentityService identityService;

	@Test
	public void test() {
		String resource = "bpm/vacation.bpmn20.xml";
		Deployment deploy = repositoryService.createDeployment().addClasspathResource(resource)
				.deploy();
		Assert.assertNotNull(deploy);
		
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
		
		String processDefinitionId = processDefinition.getId();
		Map<String, Object> variables = new HashMap<String, Object>();
		
		variables.put("initiator", "1");
		
		identityService.setAuthenticatedUserId("1");
		ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId, variables);
		
		Task task = taskService.createTaskQuery().processDefinitionId(processDefinitionId).singleResult();
		// 执行填写请假单
		taskService.complete(task.getId());
		
		Task deptLeaderAudit = taskService.createTaskQuery().processDefinitionId(processDefinitionId).singleResult();
		
		Map<String, Object> v = new HashMap<String, Object>();
		v.put("leaderComment", "同意");
		// 执行部门领导审批
		taskService.complete(deptLeaderAudit.getId(), v );
		
		Task where = taskService.createTaskQuery().processDefinitionId(processDefinitionId).singleResult();
		
		System.out.println(where);
	}
	
}
